
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Symbol和generator · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="async_await.html" />
    
    
    <link rel="prev" href="this.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="变量.html">
            
                <a href="变量.html">
            
                    
                    变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="解构.html">
            
                <a href="解构.html">
            
                    
                    解构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="字符串.html">
            
                <a href="字符串.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="数组.html">
            
                <a href="数组.html">
            
                    
                    数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="对象.html">
            
                <a href="对象.html">
            
                    
                    对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="prototype.html">
            
                <a href="prototype.html">
            
                    
                    prototype
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="Promise.html">
            
                <a href="Promise.html">
            
                    
                    Promise
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="模块化.html">
            
                <a href="模块化.html">
            
                    
                    模块化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="class和继承.html">
            
                <a href="class和继承.html">
            
                    
                    class和继承
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="this.html">
            
                <a href="this.html">
            
                    
                    this
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.13" data-path="Symbol和generator.html">
            
                <a href="Symbol和generator.html">
            
                    
                    Symbol和generator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="async_await.html">
            
                <a href="async_await.html">
            
                    
                    async_await
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="Set和WeakSet.html">
            
                <a href="Set和WeakSet.html">
            
                    
                    Set和WeakSet
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="Map和WeakMap.html">
            
                <a href="Map和WeakMap.html">
            
                    
                    Map和WeakMap
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="数字和Math.html">
            
                <a href="数字和Math.html">
            
                    
                    数字和Math
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="正则.html">
            
                <a href="正则.html">
            
                    
                    正则
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="Proxy.html">
            
                <a href="Proxy.html">
            
                    
                    Proxy
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >Symbol和generator</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="symbol&#x548C;generator">Symbol&#x548C;generator</h1>
<h1 id="symbol">Symbol</h1>
<p>Symbol&#x662F;ES6&#x63D0;&#x4F9B;&#x7684;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;, &#x6CE8;&#x610F;:</p>
<ol>
<li>Symbol&#x4E0D;&#x53EF;&#x4EE5;new</li>
<li>Symbol()&#x8FD4;&#x56DE;&#x7684;&#x662F;&#x4E00;&#x4E2A;&#x552F;&#x4E00;&#x503C;, &#x6240;&#x4EE5;&#x4E00;&#x822C;&#x4F5C;&#x4E3A;key,&#x5B9A;&#x4E49;&#x4E00;&#x4E9B;&#x552F;&#x4E00;&#x7684;&#x6216;&#x79C1;&#x6709;&#x7684;&#x4E1C;&#x897F;</li>
<li>Symbol&#x662F;&#x4E00;&#x4E2A;&#x5355;&#x72EC;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;, &#x5C31;&#x53EB;symbol, &#x662F;&#x4E00;&#x79CD;&#x57FA;&#x672C;&#x7C7B;&#x578B;(&#x5176;&#x4ED6;&#x7C7B;&#x578B;: number, string, boolean, Object, undefined, function)</li>
<li>&#x5982;&#x679C;symbol&#x4F5C;&#x4E3A;key, &#x7528;for in&#x5FAA;&#x73AF;, &#x4F1A;&#x88AB;&#x5FFD;&#x7565;</li>
</ol>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> syml = <span class="hljs-built_in">Symbol</span>(<span class="hljs-string">&quot;aaa&quot;</span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> syml); <span class="hljs-comment">// symbol</span>
</code></pre>
<pre><code class="lang-js">&lt;script&gt;
    let sym = Symbol(&quot;myKey&quot;);

    let json = {
        a:&apos;apple&apos;,
        b:&apos;banana&apos;,
        [sym]:&apos;aaa&apos;
    };
    console.log(json[&apos;a&apos;]); //apple
    console.log(json.a); //apple
    console.log(json[sym]); //aaa
    console.log(&quot;----------------&quot;);
    for (let key in json) {
        console.log(key); //sym&#x6CA1;&#x6709;&#x88AB;&#x6253;&#x5370;&#x51FA;&#x6765;
    }
&lt;/script&gt;
</code></pre>
<h1 id="generator">generator</h1>
<p>&#x4E3B;&#x8981;&#x662F;&#x4E3A;&#x4E86;&#x89E3;&#x51B3;&#x5F02;&#x6B65;&#x6DF1;&#x5EA6;&#x5D4C;&#x5957;&#x95EE;&#x9898;, &#x4E0D;&#x8FC7;&#x73B0;&#x5728;&#x57FA;&#x672C;&#x4E0A;&#x5DF2;&#x88AB; async &#x66FF;&#x4EE3;</p>
<p>&#x8BED;&#x6CD5;:</p>
<pre><code class="lang-js">&lt;script&gt;
    function * gen() { // gen &#x662F;&#x4E00;&#x4E2A;generator&#x51FD;&#x6570;
        yield &apos;welcome&apos;;
        yield &apos;to&apos;;
        return &apos;&#x4E2D;&#x56FD;&apos;;
    }
    let result = gen();
    //console.log(result.next()); //{value: &apos;welcome&apos;, done: false}
    //console.log(result.next()); //{value: &apos;to&apos;, done: false}
    //console.log(result.next()); //{value: &apos;&#x4E2D;&#x56FD;&apos;, done: true}

    //&#x4F7F;&#x7528;for of&#x5FAA;&#x73AF;
    for (let val of result) {
        //return&#x7684;&#x4E1C;&#x897F;&#x4E0D;&#x4F1A;&#x5F97;&#x5230;&#x904D;&#x5386;
        console.log(val);
        // welcome
        // to
    }
&lt;/script&gt;
</code></pre>
<p>&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x89E3;&#x6784;:</p>
<pre><code class="lang-js">&lt;script&gt;
    function * gen() { // gen &#x662F;&#x4E00;&#x4E2A;generator&#x51FD;&#x6570;
        yield &apos;welcome&apos;;
        yield &apos;to&apos;;
        yield &apos;china&apos;;
        return &apos;&#x4E2D;&#x56FD;&apos;;
    }
    let [a,b,c] = gen();
    console.log(a, b, c); //welcome to china
    let [d, ...e] = gen();
    console.log(d, e); //welcome [&apos;to&apos;, &apos;china&apos;]
    console.log(...gen()); //welcome to china
&lt;/script&gt;
</code></pre>
<pre><code class="lang-js">&lt;script src=<span class="hljs-string">&quot;https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js&quot;</span>&gt;<span class="xml"><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></span>
&lt;script&gt;
    <span class="hljs-comment">//https://api.github.com/users/ACommonChinese</span>
    <span class="hljs-function"><span class="hljs-keyword">function</span> * <span class="hljs-title">gen</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">let</span> val = <span class="hljs-keyword">yield</span> <span class="hljs-string">&apos;ACommonChinese&apos;</span>;
        <span class="hljs-keyword">yield</span> axios.get(<span class="hljs-string">`https://api.github.com/users/<span class="hljs-subst">${val}</span>`</span>);
    }
    <span class="hljs-keyword">let</span> g1 = gen();
    <span class="hljs-keyword">let</span> username = g1.next().value;
    <span class="hljs-built_in">console</span>.log(username);
    <span class="hljs-comment">// &#x8FD9;&#x91CC;&#x9762;&#x7684;username&#x4F1A;&#x4F20;&#x7ED9;val</span>
    g1.next(username).value.then(res=&gt;{
        <span class="hljs-built_in">console</span>.log(res.data);
    });
<span class="xml"><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></span>
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="this.html" class="navigation navigation-prev " aria-label="Previous page: this">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="async_await.html" class="navigation navigation-next " aria-label="Next page: async_await">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Symbol和generator","level":"1.13","depth":1,"next":{"title":"async_await","level":"1.14","depth":1,"path":"async_await.md","ref":"async_await.md","articles":[]},"previous":{"title":"this","level":"1.12","depth":1,"path":"this.md","ref":"this.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"Symbol和generator.md","mtime":"2021-12-14T17:22:49.285Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-02-17T15:54:39.156Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

